$(document).ready(function () {

	var h = Htmls;
	Array.prototype.map = function(fnc) {
		var a = new Array(this.length);
		for (var i = 0; i < this.length; i++) {
			a[i] = fnc(this[i]);
		}
		return a;
	};

	function benchmark(fun, times){
		var start = new Date().getTime();
		for(var i=0;i<times;i++) fun();
			var end = new Date().getTime();
		return (end - start) / 1000;
	}

	function dtest() { }

	test("simple div", function () {
		var d = Htmls.div();
		equals(d.toString(), "<div></div>");
	});

	test("simple input", function () {
		var i = Htmls.input();
		equals(i.toString(), "<input/>");
	});

	test("div with attr", function () {
		var d = Htmls.div({ id: "test" });
		equals(d.toString(), '<div id="test"></div>');
	});

	test("two div", function () {
		with (Htmls) {
			var d = div({id:1}, div({id:2}));
			equals(d.toString(), '<div id="1"><div id="2"></div></div>');
		}
	});

	test("div with a", function () {
		with (Htmls) {
			var d = div({id:1}, a({href:"some-href"},"some-text"));
			equals(d.toString(), '<div id="1"><a href="some-href">some-text</a></div>');
		}
	});

	test("div with text", function () {
		with (Htmls) {
			var d = div({ id: "test" }, "sometest");
			equals(d.toString(), '<div id="test">sometest</div>');
		}
	});

	test("div with list", function () {
		with (Htmls) {
			var items = ["x", "y"];


			var d = div(items.map(function (i) { return div(i); }));
			equals(d.toString(), '<div><div>x</div><div>y</div></div>');
		}
	});

	test("null support", function () {
		var d = Htmls.div(true ? null : "some text");
		equals(d.toString(), '<div></div>');
	});

	test("complete test", function () {
		with (Htmls) {
			var items = ["x", "y"];


			var d = div(
				div({ "id": "some", "class": "some-class" }, "some div"),
				"some text",

				items.map(function (i) {
					return div(i);
				}));
				equals(d.toString(),
							 '<div><div id="some" class="some-class">some div</div>some text' + 
								 '<div>x</div><div>y</div></div>');
		}
	}); 

	test("fluent complete test", function () {
		with (Htmls) {
			var items = ["x", "y"];
			var d = div(
				div().Id("some").Class("some-class").
					$("some div"), 
					"some text",
					items.map(function (i) { 
						return div(i); 
					}));
					equals(d.toString(),
								 '<div><div id="some" class="some-class">some div</div>some text' + 
									 '<div>x</div><div>y</div></div>');
		}
	}); 

	dtest("benchmark", function () {
		function test(){
			var result = Htmls.div();
			var d = result;
			for(var i=0;i < 1000; i++ ){
				var newD = h.div(h.p(h.span())).Id(i);
				d.$(newD);
				d = newD;
			}
			var r = result.toString();
		}

		ok(true, benchmark(test,100));
	});

	test("table example", function () {
		var persons = 
			[ {id:1,name:"First", balance: 100},
				{id:2,name:"Second", balance: -200},
				{id:3,name:"Third", balance: 300}];

				var tr = function(tag, items){
					return h.tr(items.map(function(x){return tag(x);}));
				};
				h.Head = function(){
					var args = Array.prototype.slice.call(arguments);
					return tr(h.th, args);
				};
				h.Row = function(){
					var args = Array.prototype.slice.call(arguments);
					return tr(h.td, args);
				};

				with (Htmls) {

					var htmlPart = table(
						Head("Name", "Balance"),
						persons.map(function(p){
							return Row(p.name, p.balance).Id(p.id).Class(p.balance > 0 ? "green" : "red" );
						}));

				}
				equals(htmlPart.toString(),
							 '<table>' + 
								 '<tr><th>Name</th><th>Balance</th></tr>' +
									 '<tr id="1" class="green"><td>First</td><td>100</td></tr>' +
										 '<tr id="2" class="red"><td>Second</td><td>-200</td></tr>' +
											 '<tr id="3" class="green"><td>Third</td><td>300</td></tr>' +
												 '</table>');
	}); 

	test("ul example", function () {

		function defaultUl(items, trans){
			if(!items.length)
				return null;  
			return h.ul(
				items.map(function(i){
				return trans(h.li(i),i);
			}));	
		}

		var items = [1,2,3];
		equals(defaultUl(items, function(tag, item){
			return tag.Id(item);
		}).toString(), '<ul><li id="1">1</li><li id="2">2</li><li id="3">3</li></ul>');

	});



});

